
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "t_types.h"
#include "drv_audio.h"
#include "hal/hal_audio.h"
#include "common/console.h"
#include "dlfcn.h"
#include "uv.h"
#include "CxlUtil.h"
#if 0

#define HAL_AUDIO_NUMSAMPLES (1024)
#define HAL_AUDIO_SAMPLERATE (8000)
#define HAL_AUDIO_RAWBUFSIZE (HAL_AUDIO_NUMSAMPLES * 2)
#define HAL_AUDIO_G711BUFSIZE (HAL_AUDIO_RAWBUFSIZE / 2)
#define HAL_AUDIO_SAMPLERATE (8000)

#define HAL_AUDIO_CAPTURE_BUF_SIZE (10 * 1024)

typedef struct _HAL_CAPTURE_CTX_S
{
	DRV_AudioHndl hAudioDrvHandle;
	T_INT32 s32CaptureRun;
	T_INT8 as8CaptureBuf[HAL_AUDIO_CAPTURE_BUF_SIZE];
} HAL_CAPTURE_CTX_S;

HAL_CAPTURE_CTX_S *gspHalAudioContext;
static T_CALLBACK gsfnCaptureCb = NULL;
static T_INT32 gsnSaveCapture = 0;
static T_CHAR gsabySaveCapPath[256];

static T_INT32 _HAL_AUDIO_CaptureThread(uv_work_t *handle);
static int _CMD_HAL_AUDIO_SaveAudioFile(CONSOLE_CMD_PARAM_S *pParam, char *pAck);

T_INT32 HAL_AUDIO_Init(void)
{
	int ret = 0;
	DRV_AudioConfig Audioconfig;
	T_INT32 nThrIdx = 0;

	gspHalAudioContext = (HAL_CAPTURE_CTX_S *)malloc(sizeof(HAL_CAPTURE_CTX_S));
	if (gspHalAudioContext == NULL)
	{
		console_error("can not malloc for gspHalAudioContext");
		return -1;
	}
	memset(gspHalAudioContext, 0, sizeof(HAL_CAPTURE_CTX_S));

	// init
	gspHalAudioContext->s32CaptureRun = 1;

	memset(&Audioconfig, 0, sizeof(Audioconfig));
	Audioconfig.deviceId = 0;
	Audioconfig.numChannels = HAL_AUDIO_CHAN_NUM;
	Audioconfig.buff_Of_Samples = HAL_AUDIO_NUMSAMPLES;
	Audioconfig.samplingRate = HAL_AUDIO_SAMPLERATE;

	ret = DRV_audioOpen(&gspHalAudioContext->hAudioDrvHandle, &Audioconfig, HAL_AUDIO_CAPTURE_DEVICE);
	if (ret != 0)
	{
		console_error("DRV_audioOpen() error\n");
		goto _error;
	}

	if (sizeof(gspHalAudioContext->as8CaptureBuf) < HAL_AUDIO_NUMSAMPLES * 2)
	{
		console_error("as8CaptureBuf too small\n");
		goto _error;
	}
	uv_work_t *hal_audio_captureReq = (uv_work_t *)malloc(sizeof(uv_work_t));
	uv_loop_t *loop = getLoop();

	uv_queue_work(loop, hal_audio_captureReq, _HAL_AUDIO_CaptureThread, NULL);

	Console_CmdAdd("t-hal-audio-save-file", _CMD_HAL_AUDIO_SaveAudioFile);

	return 0;

_error:
	HAL_AUDIO_DeInit();
	return -1;
}
T_INT32 HAL_AUDIO_DeInit(void)
{
	if (gspHalAudioContext)
	{
		DRV_audioClose(&gspHalAudioContext->hAudioDrvHandle);
		free(gspHalAudioContext);
	}

	return 0;
}
T_INT32 HAL_AUDIO_CaptureSetCallback(T_CALLBACK fnCb)
{
	return 0;
}

static T_INT32 _HAL_AUDIO_CaptureThread(uv_work_t *handle)
{
	T_INT32 s32Ret = 0;
	unsigned int numBytes = HAL_AUDIO_RAWBUFSIZE;
	int framelen = 0;

	while (gspHalAudioContext && gspHalAudioContext->s32CaptureRun)
	{
		s32Ret = DRV_audioRead(&gspHalAudioContext->hAudioDrvHandle, gspHalAudioContext->as8CaptureBuf, HAL_AUDIO_NUMSAMPLES);
		if (s32Ret == 0)
		{
			usleep(10 * 1000);
			continue;
		}
		else if (s32Ret < 0)
		{
			console_error("DRV_audioRead() error\n");
			usleep(100 * 1000);
			continue;
		}
		else if (s32Ret != (int)HAL_AUDIO_NUMSAMPLES)
		{
			console_error("Audio short read, read %d frames\n", s32Ret);
			numBytes = s32Ret * 2;
		}
		else
		{
			numBytes = HAL_AUDIO_RAWBUFSIZE;
		}
		framelen = s32Ret;

		if (gsnSaveCapture == 1)
		{
			FILE *fp = fopen(gsabySaveCapPath, "ab");
			if (fp)
			{
				fwrite(gspHalAudioContext->as8CaptureBuf, 1, numBytes, fp);
				fclose(fp);
			}
		}

		if (gsfnCaptureCb)
		{
		}
	}

	return 0;
}
static int _CMD_HAL_AUDIO_SaveAudioFile(CONSOLE_CMD_PARAM_S *pParam, char *pAck)
{
	char abyHelp[] = "./cmd [type] [file],type:0 for close,1 for save capture raw";

	if (pParam->nArgsNum != 2)
	{
		sprintf(pAck, "%s", abyHelp);
		return 0;
	}

	gsnSaveCapture = (T_INT32)strtoul(pParam->abyArgs[0], NULL, 0);
	strcpy(gsabySaveCapPath, pParam->abyArgs[1]);

	sprintf(pAck, "ok");
	return 0;
}

#endif